﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using Telerik.Web.UI;
using System.IO;

namespace Web.Description_Details
{
    public partial class EditDescriptionDetail : System.Web.UI.Page
    {
        static string prevPage = String.Empty;
        static DESCRIPTION_DETAILS dd;
        static long ddID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            if (Request.UrlReferrer != null)
            {
                prevPage = Request.UrlReferrer.ToString();
                hdntxtPrevPage.Value = prevPage;
            }
            string requestEdit = Request.QueryString["id"];
            if (requestEdit != null)
            {
                ddID = Convert.ToInt64(requestEdit);
                LoadDataDescriptionEdit();
            }
        }
        private void LoadDataDescriptionEdit()
        {
            dd = DescriptionDetailService.GetDescriptionByID(ddID);
            if (dd != null)
            {
                txtDescription.Text = dd.DESCRIPTION;
                txtDescriptionName.Text = dd.DESCRIPTION_NAME;
            }
            else
            {
                divContent.Style["display"] = "none";
                divError.Style["display"] = "block";
            }
        }
        private int updateDescription()
        {
            dd.DESCRIPTION_NAME = txtDescriptionName.Text.Trim();
            dd.DESCRIPTION = txtDescription.Text.Trim();
            return DescriptionDetailService.UpdateDescription(dd);
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            int updateDD = updateDescription();
            if (updateDD == 1)
            {
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Update Description successful !" + "','" + "DescriptionDetails.aspx" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Update Role successful !", "Roles.aspx");
            }
            else
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not update this Description !" + "');");
            //MessageBox.Show(this.Page, Page.GetType(), "Can not update Role !", "");

        }
        protected void btn_Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(prevPage);
        }
        protected void btn_Delete_ServerClick(object sender, EventArgs e)
        {
            DESCRIPTION_DETAILS dd = DescriptionDetailService.GetDescriptionByID(ddID);
            int ddDel = DescriptionDetailService.DeleteDescription(ddID);
            if (ddDel == 1)
            {
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Delete Description successful !" + "','" + "DescriptionDetails.aspx" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Delete ROLE successful !", "Roles.aspx");
            }
            else
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not delete this Description !" + "');");
            //MessageBox.Show(this.Page, Page.GetType(), "Can not delete ROLE !", "");

        }
    }
}